package com.farmpost.server.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.farmpost.server.mapper.FollowedMapper;
import com.farmpost.server.mapper.UserInfoMapper;
import com.farmpost.server.pojo.Followed;
import com.farmpost.server.pojo.RespBean;
import com.farmpost.server.pojo.UserInfo;
import com.farmpost.server.service.IFollowedService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Iterator;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author king
 * @since 2022-04-12
 */
@Service
public class FollowedServiceImpl extends ServiceImpl<FollowedMapper, Followed> implements IFollowedService {

    @Autowired
    private FollowedMapper followedMapper;
    @Autowired
    private UserInfoMapper userInfoMapper;

    /**
     * 点击关注
     *
     * @param followedUserId
     * @return
     */
    @Override
    public RespBean insertFollowed(Integer followedUserId) {
        UserInfo user = (UserInfo) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        Followed followed = new Followed();
        followed.setUserId(user.getUserId());
        followed.setFollowedUser(followedUserId);
        followed.setStatus(1);
        followed.setFollowTime(LocalDate.now());
        followed.setUpdateTime(LocalDateTime.now());
        int insert = followedMapper.insert(followed);
        if (insert == 1)
            return RespBean.success("关注成功");
        return RespBean.error("关注失败");
    }

    /**
     * 取消关注
     *
     * @param followedUserId
     * @return
     */
    @Override
    public RespBean cancelFollowed(Integer followedUserId) {
        int updateStatus = followedMapper.updateStatus(followedUserId, 0);
        if (updateStatus == 1)
            return RespBean.success("取消关注成功");
        return RespBean.error("取消关注失败");
    }

    /**
     * 查询关注
     *
     * @return
     */
    @Override
    public List<Followed> getFollowed() {
        UserInfo user = (UserInfo) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        List<Followed> followedes = followedMapper.selectList(new QueryWrapper<Followed>().eq("user_id", user.getUserId()));
        for (Followed followed : followedes) {
            Integer followedUserId = followed.getFollowedUser();
            UserInfo userInfo = userInfoMapper.selectOne(new QueryWrapper<UserInfo>().eq("user_id", followedUserId));
            userInfo.setUserPassword(null);
            followed.setUserInfo(userInfo);
        }
        return followedes;
    }

    /**
     * 更改关注信息
     *
     * @param userId
     * @param followedUserId
     * @param status
     * @return
     */
    @Override
    public RespBean updateFollowed(Object userId, Object followedUserId, Object status) {
        //用户存在才能更改
        UserInfo userInfo = userInfoMapper.selectOne(new QueryWrapper<UserInfo>().eq("user_id", userId));
        if (userInfo.getUserId() != null) {
            if (followedUserId != null && status == null) {
                int updateFollowedUserId = followedMapper.updateFollowedUserId((Integer) followedUserId, (Integer) userId, LocalDateTime.now());
                if (updateFollowedUserId == 1)
                    return RespBean.success("更改用户关注者id成功");
                else
                    return RespBean.error("更改用户关注者id失败");
            } else if (followedUserId == null && status != null) {
                int updateFollowedStatus = followedMapper.updateFollowedStatus((Integer) status, (Integer) userId, LocalDateTime.now());
                if (updateFollowedStatus == 1)
                    return RespBean.success("更改关注者状态成功");
                return RespBean.error("更改关注者状态失败");
            } else if (followedUserId != null && status != null) {
                int updateFollowed = followedMapper.updateFollowed((Integer) followedUserId, (Integer) status, (Integer) userId, LocalDateTime.now());
                if (updateFollowed == 1)
                    return RespBean.success("更改关注信息成功");
                return RespBean.error("更改关注信息失败");
            } else {
                return RespBean.error("未输入要更改信息");
            }
        } else {
            return RespBean.error("更改用户不存在");
        }
    }
}
